﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VIP.Extension;
using ZXing;
using ZXing.Common;

public class VIPQRUtil {

    /// <summary>
    /// 生成二维码(不带中心图）
    /// </summary>
    /// <param name="content">内容</param>
    /// <param name="width">宽度</param>
    /// <param name="height">高度</param>
    /// <param name="centerIcon">中心图片</param>
    /// <returns>二维码图片</returns>
    public static Texture2D GenerateQRCode(string content, int width, int height)
    {
        // 编码成color32
        MultiFormatWriter writer = new MultiFormatWriter();
        Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
        hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.Add(EncodeHintType.MARGIN, 1);
        hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        // 转成texture2d
        Texture2D texture = VIPConvertUtil.BitMatrixToTexture2D(bitMatrix);
        texture.Apply();

        return texture;
    }


    /// <summary>
    /// 生成二维码(带中心图）
    /// </summary>
    /// <param name="content">内容</param>
    /// <param name="width">宽度</param>
    /// <param name="height">高度</param>
    /// <param name="centerIcon">中心图片</param>
    /// <returns>二维码图片</returns>
    public static Texture2D GenerateQRCode(string content, int width, int height, Texture2D centerIcon)
    {
        // 编码成color32
        MultiFormatWriter writer = new MultiFormatWriter();
        Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
        hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.Add(EncodeHintType.MARGIN, 1);
        hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        // 转成texture2d
        Texture2D texture = VIPConvertUtil.BitMatrixToTexture2D(bitMatrix);

        if (centerIcon.IsNull()) {
            Debug.LogError("二维码创建失败，中心图片不能为空");
            return null;
        }

        // 添加小图
        int halfWidth = texture.width / 2;
        int halfHeight = texture.height / 2;
        int halfWidthOfIcon = centerIcon.width / 2;
        int halfHeightOfIcon = centerIcon.height / 2;
        int centerOffsetX = 0;
        int centerOffsetY = 0;
        for (int x = 0; x < bitMatrix.Height; x++)
        {
            for (int y = 0; y < bitMatrix.Width; y++)
            {
                centerOffsetX = x - halfWidth;
                centerOffsetY = y - halfHeight;
                if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon)
                {
                    texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon));
                }
            }
        }
        texture.Apply();
        return texture;
    }

}
